﻿// See https://aka.ms/new-console-template for more information
// Object 对象转换为 Json String

//string json = @"{
//            id:'1',
//            name:'小明',
//            age:'12'
//        }";

using Newtonsoft.Json;
using System.Data;

//User user = new User { Id = "1", Name = "小明", Age = "12" };

//string userJson = JsonConvert.SerializeObject(user);

//string userJson1 = user.ToJson();

//User userTemp=JsonConvert.DeserializeObject<User>(userJson);

//User userTemp1=userJson1.ToObject<User>();

//Console.WriteLine(userJson);

DataTable userTable = new DataTable("user");
userTable.Columns.Add("Id");
userTable.Columns.Add("Name");
userTable.Columns.Add("Age");

userTable.Rows.Add(1, "小明", 12);
userTable.Rows.Add(2, "小红", 11);
userTable.Rows.Add(3, "小刚", 13);

string userTableJson=userTable.ToJson();

Console.WriteLine(userTableJson);

List<User> userList=userTableJson.ToObject<List<User>>();


//List<User> userList = new List<User>();
//for (int i = 0; i < userTable.Rows.Count; i++)
//{
//    DataRow row = userTable.Rows[i];
//    userList.Add(new User
//    {
//        Id = row["Id"].ToString(),
//        Name = row["Name"].ToString(),
//        Age = row["Age"].ToString(),
//    });
//}


Console.ReadLine();

static class MyObject
{
    public static string ToJson(this Object obj)
    {
        return JsonConvert.SerializeObject(obj);
    }
}

static class MyString
{
    public static T ToObject<T>(this string str)
    {
        return JsonConvert.DeserializeObject<T>(str);
    }
}

class User
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Age { get; set; }
}
